package com.lpit.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lpit.constant.Constant;
import com.lpit.dto.ChannelDto;
import com.lpit.pojo.Article;
import com.lpit.pojo.Channel;
import com.lpit.service.ArticleService;
import com.lpit.service.ChannelService;
import com.lpit.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 石智星
 * @date 2023/6/13
 * 频道管理
 */
@RestController
@RequestMapping("/channel")
@Slf4j
@Api(value = "频道管理",description = "管理员操作")
public class ChannelController {

    @Autowired
    private ChannelService channelService;
    @Autowired
    private ArticleService articleService;

    /**
     * 查询所有
     * 使用分页插件，向前端页面展示数据
     * @Page 两个参数分别是当前页码，每页显示数量
     * @return
     */
    @GetMapping("/list/{page}/{pageSize}")
    @ApiOperation("分页查询所有信息")
    public Result<Page<ChannelDto>> list(@PathVariable Integer page, @PathVariable Integer pageSize, String name, Integer state){
        log.info("当前页码{}",page);
        log.info("每页显示的数量{}",pageSize);
        //创建分页构造器
        Page<Channel> channelPage = new Page<>(page,pageSize);

        LambdaQueryWrapper<Channel> channelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据频道名称进行模糊查询
        channelLambdaQueryWrapper.like(name != null && !"".equals(name),Channel::getChannelName,name);
        //根据频道状态进行查询
        channelLambdaQueryWrapper.eq(state!=null,Channel::getChannelStatus,state);
        //根据频道编号排序
        channelLambdaQueryWrapper.orderByAsc(Channel::getChannelNumber);

        //执行查询条件
        channelService.page(channelPage,channelLambdaQueryWrapper);
        //创建前端需要的分页构造器
        Page<ChannelDto> channelDtoPage = new Page<>();
        //进行对象的拷贝
        BeanUtils.copyProperties(channelPage,channelDtoPage);
        //获取Channe实体类信息
        List<Channel> channelList = channelPage.getRecords();
        //创建list集合给新的分页构造器
        List<ChannelDto> channelDtoList = new ArrayList<>();
        //遍历
        channelList.forEach(channel -> {
            //创建前端需要的实体类对象
            ChannelDto channelDto = new ChannelDto();
            //对象拷贝
            BeanUtils.copyProperties(channel,channelDto);
            //获取channel表的id
            Integer id = channel.getId();
            //构造查询条件
            LambdaQueryWrapper<Article> articleLambdaQueryWrapper = new LambdaQueryWrapper<>();
            articleLambdaQueryWrapper.eq(Article::getChannelId,id);
            //获取有多少条数据
            long count = articleService.count(articleLambdaQueryWrapper);
            //实体内赋值
            channelDto.setArticleNumber(count);
            //添加到集合
            channelDtoList.add(channelDto);
        });
        //将集合添加到新的分页构造器
        channelDtoPage.setRecords(channelDtoList);
        //返回数据到前端
        return Result.success(channelDtoPage);
    }
    @GetMapping("/{id}")
    public Result<Channel> query(@PathVariable Long id){

        Channel channel = channelService.getById(id);
        return Result.success(channel);
    }

    /**
     * 新增一个频道
     * @return
     */
    @PostMapping
    @ApiOperation("新增频道")
    public Result<String> save(@RequestBody Channel channel){
        log.info("新增频道信息{}",channel);
        //查询所有的频道信息
        List<Channel> channelList = channelService.list();
        //遍历
        for (Channel channel1: channelList) {
            //判断频道编号是否重复
            if (channel1.getChannelNumber().equals(channel.getChannelNumber())){
                return Result.error(Constant.DUPLICATE_CHANNEL_NUMBER);
            }
            //判断频道名字是否重复
            if (channel1.getChannelName().equals(channel.getChannelName())){
                return Result.error(Constant.CHANNEL_NAME_DUPLICATION);
            }
        }
        //新增频道
        channelService.save(channel);
        return Result.success(Constant.INCREASE_SUCCESS);
    }

    /**
     * 修改频道信息
     * @return
     */
    @PutMapping
    @ApiOperation("修改频道")
    public Result<String> modify(@RequestBody Channel channel){
        //记录修改信息
        log.info("修改频道信息{}",channel);
//        LambdaUpdateWrapper<Channel> channelLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
//        channelLambdaUpdateWrapper.eq(Channel::getChannelNumber,channel.getChannelNumber());
        //执行修改操作
        boolean update = channelService.updateById(channel);//https://nmda7k9u.mirror.aliyuncs.com
        //返回结果
        return update ? Result.success(Constant.MODIFY_SUCCESSFULLY) : Result.error(Constant.FAIL_TO_MODIFY);
    }

    /**
     * 删除频道信息
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除频道")
    public Result<String> delete(@PathVariable Integer id){
        //记录删除频道的编码
        log.info("删除频道的编码{}",id);
        //创建构造条件
//        LambdaUpdateWrapper<Channel> channelLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
//        channelLambdaUpdateWrapper.eq(Channel::getChannelNumber,id);
        //执行删除操作
        boolean remove = channelService.removeById(id);
        //返回结果
        return remove ? Result.success(Constant.SUCCESSFULLY_DELETE) : Result.error(Constant.FAIL_TO_DELETE);
    }
}
